package getResistantData;

import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvException;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @projectName: code-snippet
 * @className: GrabDataFromCSV
 * @description:
 * @author: JLONG
 * @date: 2024/8/22 14:12
 * @version: 1.0
 */
public class GrabDataFromCSV {
    public static void main(String[] args) {
        GrabDataFromCSV.grabResistance(
                "",
                "../Resistant.csv");
                }

    public static void grabResistance(String inputFilePath, String outputFilePath) {
        grabResistance(new File(inputFilePath), new File(outputFilePath));
    }
    public static void grabResistance(File inputFile, File outputFile){
        List<MetaData> list = new ArrayList<>();
        try {
            CSVReader reader = new CSVReader(new FileReader(inputFile));
            List<String[]> records = reader.readAll();
            for (String[] record : records) {
                for (String str: record) {
                    if (str.equals("BATTERY_RESISTANCE_SENSOR")) {
                        String labelID  = record[6];
                        String sensorValue = record[18].substring(4);
                        String readableSensorValue = record[19].substring(4);
                        list.add(new MetaData(labelID, sensorValue, readableSensorValue));
                    }
                }
            }
            reader.close();
            Map<String, List<MetaData>> groupByLabelID = list.stream().collect(Collectors.groupingBy(MetaData::getLabelID));
            Map<String, String> readableSensorValues = getSensorValues(groupByLabelID, true);
            for (String str : readableSensorValues.keySet()) {
                System.out.println(str + ": " + readableSensorValues.get(str));
            }
            writeMapToCSV(readableSensorValues, outputFile);

        } catch (CsvException | IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static Map<String, String> getSensorValues(Map<String, List<MetaData>> groupByLabelID, boolean isReadableSensorValue) {
        Map<String, String> sensorValues = new HashMap<>();
        for (String labelID : groupByLabelID.keySet()) {
            List<MetaData> data = groupByLabelID.get(labelID);
            List<String> sensors = new ArrayList<>();
            for (MetaData metaData : data) {
                if (isReadableSensorValue) {
                    sensors.add(String.valueOf(metaData.getReadableSensorValue()));
                } else {
                    sensors.add(String.valueOf(metaData.getSensorValue()));
                }
            }
            sensorValues.put(labelID, sensors.stream().map(Object::toString).collect(Collectors.joining(", ")));
        }
        return sensorValues;
    }

    public static void writeMapToCSV(Map<String, String> map, File outputFile) {
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile));
            bw.write("Label ID,Resistance Value"+ "\n");
            for (String labelID : map.keySet()) {
                bw.write(labelID + "," + map.get(labelID) + "\n");
            }
            bw.flush();
            bw.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
